package com.yc.internet;

import javax.swing.*;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Arrays;
import java.util.Scanner;

public class QQClient {
    public static void main(String[] args) throws IOException, InterruptedException {
        //创建之后立即连接=》返回 socket
        Socket socket=new Socket("127.0.01",8888);
        System.out.println("连接成功");


        InetAddress inetAddress=socket.getInetAddress();
        System.out.println("主机地址"+inetAddress.getHostAddress());
        byte [] address=inetAddress.getAddress();
        System.out.println("IP地址"+ Arrays.toString(address));
        System.out.println("主机名"+inetAddress.getHostAddress());



        InputStream in=socket.getInputStream();
        OutputStream out=socket.getOutputStream();

        //屏幕输入对象
        Scanner scanner=new Scanner(System.in);

        Thread readThread=new Thread(()->{

            boolean running=true;
            while(running){
                try {
                    recv(in);
                } catch (IOException e) {
                    throw new RuntimeException();
                }
            }
        });

        Thread writeThread=new Thread(()->{

            boolean running=true;
            while(running){
                Object data=input(scanner);
                try {
                    send(data,out);
                } catch (IOException e) {
                    throw new RuntimeException();
                }

            }
        });



        readThread.start();
        writeThread.start();

        //等待读写线程结束，主线程才执行关闭的代码
        readThread.join();
        writeThread.join();
        //Unreachable statement
        in.close();
        out.close();
        socket.close();
    }


    //用户输入文字或文件
    public static Object input(Scanner scanner){
        System.out.println("请选择发送文字（1)还是文件（2）：");
        String s=scanner.nextLine();
        if("2".equals(s)){
            JFileChooser jFileChooser=new JFileChooser();
            int i=jFileChooser.showOpenDialog(null);
            if(i==JFileChooser.APPROVE_OPTION){
                //用户选择了一个文件，返回选择的文件
                return jFileChooser.getSelectedFile();
            }

        }else{
            System.out.println("请输入文字：");
            return scanner.nextLine();
        }
        return null;
    }


    public static void sendText(String msg,OutputStream out) throws IOException {
        DataOutputStream dos=new DataOutputStream(out);
        dos.writeInt(1);
        out.write(msg.getBytes());
    }

    public static  void sendFile(File file,OutputStream out) throws IOException {
        DataOutputStream dos=new DataOutputStream(out);
        dos.writeInt(2);
        //发送文件名
        dos.writeUTF(file.getName());
        //发送文件大小
        dos.writeLong(file.length());
        //发送文件内容
        try(FileInputStream fis=new FileInputStream(file)) {
            byte[] bytes=new byte[1024];
            int count;
            while ((count=fis.read(bytes))>0){
                out.write(bytes,0,count);
            }
        }
    }

    public static void recv(InputStream in) throws IOException {
        DataInputStream dis=new DataInputStream(in);
        int i=dis.readInt();
        if(i==1){
            byte[] bytes=new byte[1024];
            int count;
            count=in.read(bytes);
            System.out.println(count);
            String msg=new String(bytes,0,count);
            System.out.println("服务段说："+msg);
        }else if(i==2){
            String filename=dis.readUTF();
            long length = dis.readLong();//文件大小
            System.out.println("接受到文件："+filename);
            String filepath="E:\\文件柜\\"+filename;
            try(FileOutputStream fos=new FileOutputStream(filepath)) {
                byte [] bytes=new byte[1024];
                int count;
                long readLength=0;
                while ((count=in.read(bytes))>0){
                    readLength+=count; //记录读取的字节数
                    fos.write(bytes,0,count);
                    //读完就退出
                    if(readLength>=length){
                        break;
                    }
                }
            }
            System.out.println("文件保存到:"+filepath);
        }


    }

    public static void send(Object object,OutputStream out) throws IOException {
        if(object instanceof String){
            String msg=(String)object;
            sendText(msg,out);
        }else if(object instanceof File){
            File file=(File) object;
            sendFile(file,out);
        }else {
            System.out.println("未知数据格式："+object);
        }

    }

}
    //

